package com.cbj.db_work.controller;


import com.cbj.db_work.common.result.R;
import com.cbj.db_work.entity.News;
import com.cbj.db_work.entity.vo.NewsVo;
import com.cbj.db_work.service.NewsService;
import com.cbj.db_work.valid.AddGroup;
import com.cbj.db_work.valid.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cbj
 * @since 2021-07-03
 */
@RestController
@RequestMapping("/db_work/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    @PostMapping("/addNews")
    public R addNews(@Validated({AddGroup.class}) @RequestBody NewsVo vo){
        boolean flag = newsService.addNews(vo);
        if(flag)
            return R.ok();
        return R.error();
    }
    @GetMapping("/getNewsTopSix")
    public R getNewsTopSix(){
        List<News> list = newsService.getNewsTopSix();
        return R.ok().data("list",list);
    }
    @GetMapping("/getNewById/{id}")
    public R getNewById(@PathVariable String id){
        News byId = newsService.getById(id);
        return R.ok().data("news",byId);

    }

}

